package com.ev.aslReport.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.asl.constant.AslConstant;
import com.ev.aslReport.service.ReportProcessingDataService;
import com.ev.aslReport.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: π
 * @date 2022/7/22 16:22
 */
@Slf4j
@Service
public class ReportProcessingDataServiceImpl implements ReportProcessingDataService {

    private Date dealDateFormatOfNow(String nowDateStr) {
        try {
            SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date parse = dateParser.parse(nowDateStr);
            return parse;
        } catch (Exception e) {
            log.info("---------------now--时间转换出错：" + e.getMessage());
            log.info(e.getMessage());
        }
        return null;
    }
    private String dealDateFormatOfNowOfString(String nowDateStr) {
        try {
            SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date parse = dateParser.parse(nowDateStr);
            SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
            String dateStrEnd = ss.format(parse);
            return dateStrEnd;
        } catch (Exception e) {
            log.info("---------------now--时间转换出错：" + e.getMessage());
            log.info(e.getMessage());
        }
        return null;
    }

    private String dealDateFormatOfNowOfStringJudge(String nowDateStr) {
        if(nowDateStr.indexOf("T")>-1){
            return  dealDateFormatOfNowOfString(nowDateStr);
        }else{
            return nowDateStr;
        }
    }




    // 物料收发明细
    public Triple<Boolean, Integer, JSONArray> processingDataOfOne(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, Map<String, BigDecimal> balanceCountMap, Map<String, BigDecimal> inCountMap, Map<String, BigDecimal> outCountMap, String materialModel, String materialNumber) {
        JSONArray right = remoteDataUnderway.getRight();
        log.info("----balanceCountMap  {}}：",balanceCountMap);
        log.info("----inCountMap  {}}：",inCountMap);
        List<reportOneVO> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    //过滤没有单据号的数据
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    if (!value.containsKey("单据号") || ObjectUtil.isEmpty(value.get("单据号"))){
                        continue;
                    }
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNumberTemp = value.containsKey("物料代码") ? value.get("物料代码").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNumberTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialNumber)? "":materialNumber.toLowerCase())){
                        continue;
                    }
                    reportOneVO vo = new reportOneVO();
                    log.info("---------------解析：------年份-" + value.get("年份"));
                    vo.setPeriod(value.containsKey("年份") ? value.get("年份").toString() : "");
                    vo.setInOutTime(value.containsKey("日期") ? this.dealDateFormatOfNowOfStringJudge(value.get("日期").toString()) : null);
                    vo.setBatch(value.containsKey("批号") ? value.get("批号").toString() : "");
                    vo.setCode(value.containsKey("单据号") ? value.get("单据号").toString() : "");
                    vo.setSpecification(value.containsKey("规格型号") ? value.get("规格型号").toString() : "");
                    vo.setName(value.containsKey("物料名称") ? value.get("物料名称").toString() : "");
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setStorageTypeName(value.containsKey("单据类型") ? value.get("单据类型").toString() : "");
                    vo.setInCount(new BigDecimal(value.containsKey("入库数量") ? value.get("入库数量").toString() : "0").setScale(2));
                    vo.setInUnitPrice(new BigDecimal(value.containsKey("入库单价") ? value.get("入库单价").toString() : "0").stripTrailingZeros());
                    vo.setInUnitPrice(new BigDecimal(value.containsKey("入库金额") ? value.get("入库金额").toString() : "0").stripTrailingZeros());
                    vo.setOutCount(new BigDecimal(value.containsKey("出库数量") ? value.get("出库数量").toString() : "0").setScale(2));
                    vo.setOutUnitPrice(new BigDecimal(value.containsKey("出库单价") ? value.get("出库单价").toString() : "0").stripTrailingZeros());
                    vo.setOutAmount(new BigDecimal(value.containsKey("出库金额") ? value.get("出库金额").toString() : "0").stripTrailingZeros());
                    vo.setBalanceCount(new BigDecimal(value.containsKey("结存数量") ? value.get("结存数量").toString() : "0").setScale(2));
                    vo.setBalanceUnitPrice(new BigDecimal(value.containsKey("结存单价") ? value.get("结存单价").toString() : "0").stripTrailingZeros());
                    vo.setBalanceAmount(new BigDecimal(value.containsKey("结存金额") ? value.get("结存金额").toString() : "0").stripTrailingZeros());
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    if (ObjectUtil.isNotEmpty(balanceCountMap) && balanceCountMap.containsKey(vo.getMaterieCode())) {
                        vo.setInCountSum(balanceCountMap.get(vo.getMaterieCode()));
                    }
                    if (ObjectUtil.isNotEmpty(outCountMap) && outCountMap.containsKey(vo.getMaterieCode())) {
                        vo.setOutCountSum(outCountMap.get(vo.getMaterieCode()));
                    }
                    if (ObjectUtil.isNotEmpty(inCountMap) && inCountMap.containsKey(vo.getMaterieCode())) {
                        if (ObjectUtil.isNotEmpty(balanceCountMap) && balanceCountMap.containsKey(vo.getMaterieCode())) {
                            vo.setBalanceCountSum(balanceCountMap.get(vo.getMaterieCode()));
                        }
                    }
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----one  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<reportOneVO> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
            //return Triple.of(false, remoteDataUnderway.getMiddle(), null);
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }
    // 物料收发明细
    public List<reportOneVO> processingDataOfOne2(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, String materialModel, String materialNumber) {
        JSONArray right = remoteDataUnderway.getRight();
        List<reportOneVO> collect = new ArrayList<>();
        List<reportOneVO> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    reportOneVO vo = new reportOneVO();
                    log.info("---------------解析：------年份-" + value.get("年份"));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNumberTemp = value.containsKey("物料代码") ? value.get("物料代码").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNumberTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialNumber)? "":materialNumber.toLowerCase())){
                        continue;
                    }
                    vo.setPeriod(value.containsKey("年份") ? value.get("年份").toString() : "");
                    vo.setInOutTime(value.containsKey("日期") ? this.dealDateFormatOfNowOfStringJudge(value.get("日期").toString()) : null);
                    vo.setBatch(value.containsKey("批号") ? value.get("批号").toString() : "");
                    vo.setCode(value.containsKey("单据号") ? value.get("单据号").toString() : "");
                    vo.setSpecification(value.containsKey("规格型号") ? value.get("规格型号").toString() : "");
                    vo.setName(value.containsKey("物料名称") ? value.get("物料名称").toString() : "");
                    vo.setStorageTypeName(value.containsKey("单据类型") ? value.get("单据类型").toString() : "");
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setInCount(new BigDecimal(value.containsKey("入库数量") ? value.get("入库数量").toString() : "0"));
                    vo.setInUnitPrice(new BigDecimal(value.containsKey("入库单价") ? value.get("入库单价").toString() : "0"));
                    vo.setInUnitPrice(new BigDecimal(value.containsKey("入库金额") ? value.get("入库金额").toString() : "0"));
                    vo.setOutCount(new BigDecimal(value.containsKey("出库数量") ? value.get("出库数量").toString() : "0"));
                    vo.setOutUnitPrice(new BigDecimal(value.containsKey("出库单价") ? value.get("出库单价").toString() : "0"));
                    vo.setOutAmount(new BigDecimal(value.containsKey("出库金额") ? value.get("出库金额").toString() : "0"));
                    vo.setBalanceCount(new BigDecimal(value.containsKey("结存数量") ? value.get("结存数量").toString() : "0"));
                    vo.setBalanceUnitPrice(new BigDecimal(value.containsKey("结存单价") ? value.get("结存单价").toString() : "0"));
                    vo.setBalanceAmount(new BigDecimal(value.containsKey("结存金额") ? value.get("结存金额").toString() : "0"));
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----one  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return collect;
            }
        }
        return collect;
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfTwo(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, Map<String, BigDecimal> storageQtySMap, String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportTwoVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    ReportTwoVo vo = new ReportTwoVo();
                    String StartDate = value.get("开始时间").toString();
//                    Date date = this.dealDateFormatOfNow(value.get("开始时间").toString());
//                    log.info("-------------StartDate:"+StartDate);
//                    log.info("-------------date:"+date);
                    vo.setStartDate(value.containsKey("开始时间") ? value.get("开始时间").toString() : null);
                    vo.setEndDate(value.containsKey("结束时间") ? value.get("结束时间").toString() : null);
                    vo.setStorageDate(value.containsKey("入库日期") ? value.get("入库日期").toString() : null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setBillNo(value.containsKey("单据编号") ? value.get("单据编号").toString() : null);
                    vo.setBillType(value.containsKey("单据类型") ? value.get("单据类型").toString() : null);
                    vo.setOriginalSingleType(value.containsKey("源单类型") ? value.get("源单类型").toString() : null);
                    vo.setSourceNo(value.containsKey("来源单号") ? value.get("来源单号").toString() : null);
                    vo.setStorageQty(new BigDecimal(value.containsKey("入库数量") ? value.get("入库数量").toString() : "0").setScale(2));

                    vo.setStockQty(new BigDecimal(value.containsKey("库存数量") ? value.get("库存数量").toString() : "0").setScale(2));
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() :null);
                    vo.setIncomingPart(value.containsKey("入库员") ? value.get("入库员").toString() : null);
                    if (ObjectUtil.isNotEmpty(storageQtySMap) && storageQtySMap.containsKey(vo.getMaterieCode())) {
                        vo.setStorageQtySum(storageQtySMap.get(vo.getMaterieCode()));
                    }
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----2  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportTwoVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }
    @Override
    public List<ReportTwoVo> processingDataOfTwo2(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportTwoVo> collect = new ArrayList<>();
        List<ReportTwoVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    ReportTwoVo vo = new ReportTwoVo();
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    String StartDate = value.get("开始时间").toString();
                    vo.setStartDate(value.containsKey("开始时间") ? value.get("开始时间").toString() : null);
                    vo.setEndDate(value.containsKey("结束时间") ? value.get("结束时间").toString() : null);
                    vo.setStorageDate(value.containsKey("入库日期") ? value.get("入库日期").toString() : null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setBillNo(value.containsKey("单据编号") ? value.get("单据编号").toString() : null);
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setBillType(value.containsKey("单据类型") ? value.get("单据类型").toString() : null);
                    vo.setOriginalSingleType(value.containsKey("源单类型") ? value.get("源单类型").toString() : null);
                    vo.setSourceNo(value.containsKey("来源单号") ? value.get("来源单号").toString() : null);
                    vo.setStorageQty(new BigDecimal(value.containsKey("入库数量") ? value.get("入库数量").toString() : "0").setScale(2));

                    vo.setStockQty(new BigDecimal(value.containsKey("库存数量") ? value.get("库存数量").toString() : "0").setScale(2));
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() :null);
                    vo.setIncomingPart(value.containsKey("入库员") ? value.get("入库员").toString() : null);

                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----2  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return collect;
            }
        }
        return collect;
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfThree(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, Map<String, BigDecimal> outStockQtySMap,String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportThreeVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    ReportThreeVo vo = new ReportThreeVo();
                    vo.setStartDate(value.containsKey("开始时间") ?value.get("开始时间").toString() : null);
                    vo.setEndDate(value.containsKey("结束时间") ? value.get("结束时间").toString(): null);
                    vo.setOutStockDate(value.containsKey("出库日期") ? value.get("出库日期").toString(): null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setBillNo(value.containsKey("单据编号") ? value.get("单据编号").toString() : null);
                    vo.setBillType(value.containsKey("单据类型") ? value.get("单据类型").toString() : null);
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setOutStockQty(new BigDecimal(value.containsKey("出库数量") ? value.get("出库数量").toString() : "0").setScale(2));
                    vo.setStockQty(new BigDecimal(value.containsKey("库存数量") ? value.get("库存数量").toString() : "0").setScale(2));
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() : null);
                    vo.setStockOutAssistant(value.containsKey("出库员") ? value.get("出库员").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    if (ObjectUtil.isNotEmpty(outStockQtySMap) && outStockQtySMap.containsKey(vo.getMaterieCode())) {
                        vo.setOutStockQtySum(outStockQtySMap.get(vo.getMaterieCode()));
                    }
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----3  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportThreeVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }
    @Override
    public List<ReportThreeVo> processingDataOfThree2(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportThreeVo> collect = new ArrayList<>();
        List<ReportThreeVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    ReportThreeVo vo = new ReportThreeVo();
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    vo.setStartDate(value.containsKey("开始时间") ?value.get("开始时间").toString() : null);
                    vo.setEndDate(value.containsKey("结束时间") ? value.get("结束时间").toString(): null);
                    vo.setOutStockDate(value.containsKey("出库日期") ? value.get("出库日期").toString(): null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setBillNo(value.containsKey("单据编号") ? value.get("单据编号").toString() : null);
                    vo.setBillType(value.containsKey("单据类型") ? value.get("单据类型").toString() : null);
                    vo.setMaterieCode(value.containsKey("物料代码") ? value.get("物料代码").toString() : "");
                    vo.setOutStockQty(new BigDecimal(value.containsKey("出库数量") ? value.get("出库数量").toString() : "0"));
                    vo.setStockQty(new BigDecimal(value.containsKey("库存数量") ? value.get("库存数量").toString() : "0"));
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() : null);
                    vo.setStockOutAssistant(value.containsKey("出库员") ? value.get("出库员").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----3  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return collect;
            }
        }
        return collect;
    }

    public static void main(String[] args) {

        BigDecimal bigDecimal = BigDecimal.ZERO;
        BigDecimal aaa = new BigDecimal("42");
        BigDecimal bbb = new BigDecimal("1000");
        DecimalFormat df = new DecimalFormat("0.000%");
        if (ObjectUtil.isEmpty(aaa) || ObjectUtil.isEmpty(bbb)
                || bbb.compareTo(BigDecimal.ZERO) <= 0 || aaa.compareTo(BigDecimal.ZERO) <= 0){
        }else {
            System.out.println(df.format(aaa.divide(bbb)));;
        }
        System.out.println(df.format(BigDecimal.ZERO));;
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfFour(Triple<Boolean, Integer, JSONArray> remoteDataUnderway,String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportFourVo> result = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.000%");
        BigDecimal variableQty = BigDecimal.ZERO;
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
//                try {
                JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                    continue;
                }
                if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                    continue;
                }
                ReportFourVo vo = new ReportFourVo();
                vo.setMaterialClass(value.containsKey("物料类别") ? value.get("物料类别").toString() : "");
                vo.setBatchNo(value.containsKey("批次") ? value.get("批次").toString() : "");
                vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                vo.setSafetyStock(new BigDecimal(value.containsKey("安全库存量") ? value.get("安全库存量").toString() : "0").setScale(2));
                vo.setActualInventory(new BigDecimal(value.containsKey("实际库存量") ? value.get("实际库存量").toString() : "0").setScale(2));

                //实际库存/安全库存
                if (ObjectUtil.isEmpty(vo.getSafetyStock()) || ObjectUtil.isEmpty(vo.getActualInventory())
                        || vo.getActualInventory().compareTo(BigDecimal.ZERO) <= 0 || vo.getSafetyStock().compareTo(BigDecimal.ZERO) <= 0){
                    vo.setVariableQty(df.format(BigDecimal.ZERO));
                    log.info("====1111111111111,actualInventory:{},safetyStock:{}",vo.getActualInventory(),vo.getSafetyStock());
                    log.info("====vo.getActualInventory().compareTo(BigDecimal.ZERO):{}",vo.getActualInventory().compareTo(BigDecimal.ZERO));
                    log.info("====vo.getSafetyStock().compareTo(BigDecimal.ZERO):{}",vo.getSafetyStock().compareTo(BigDecimal.ZERO) );
                }else {
                    variableQty = vo.getActualInventory().divide(vo.getSafetyStock(),BigDecimal.ROUND_HALF_UP);
                    vo.setVariableQty(df.format(variableQty));
                    log.info("====222222222222,actualInventory:{},safetyStock:{}",vo.getActualInventory(),vo.getSafetyStock());
                }
//                    vo.setVariableQty(df.format(new BigDecimal(value.containsKey("变化量") ? value.get("变化量").toString() : "0")));
                vo.setStatus(value.containsKey("状态") ? value.get("状态").toString() : null);
                vo.setWarehouseEntryTime(value.containsKey("入库时间") ? this.dealDateFormatOfNowOfStringJudge(value.get("入库时间").toString()) : null);
                vo.setRemark(value.containsKey("备注") ? value.get("备注").toString() : null);
                vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                vo.setStockName(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                result.add(vo);
//                } catch (Exception e) {
//                    log.info("----4  数据解析出现异常：");
//                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportFourVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfFive(Triple<Boolean, Integer, JSONArray> remoteDataUnderway,String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        List<ReportFiveVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    ReportFiveVo vo = new ReportFiveVo();
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setSafeStock(new BigDecimal(value.containsKey("安全库存量") ? value.get("安全库存量").toString() : "0").setScale(2));
                    vo.setStock(new BigDecimal(value.containsKey("库存") ? value.get("库存").toString() : "0").setScale(2));
                    vo.setScheduledReceipt(new BigDecimal(value.containsKey("预计入库") ? value.get("预计入库").toString() : "0").setScale(2));
                    vo.setAllocation(new BigDecimal(value.containsKey("已分配量") ? value.get("已分配量").toString() : "0").setScale(2));
                    vo.setLockLibraryAmount(new BigDecimal(value.containsKey("锁库量") ? value.get("锁库量").toString() : "0").setScale(2));
                    vo.setAvailableStock(new BigDecimal(value.containsKey("可用库存") ? value.get("可用库存").toString() : "0").setScale(2));
                    vo.setProcurementCycle(value.containsKey("采购周期") ? sd.format(sd.parse(value.get("采购周期").toString()))  : null);
                    vo.setRemark(value.containsKey("备注") ? value.get("备注").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString().trim() : "0"));
                    vo.setStockName(value.containsKey("库存仓库") ? value.get("库存仓库").toString() : null);
                    if(vo.getAllocation().intValue()==AslConstant.ZERO && vo.getLockLibraryAmount().intValue()!= AslConstant.ZERO){
                        vo.setAllocation(vo.getLockLibraryAmount());
                    }
                    result.add(vo);
                } catch (Exception e) {
                    log.info("error:"+e);
                    log.info("----5  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportFiveVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfSix(Triple<Boolean, Integer, JSONArray> remoteDataUnderway,String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportSixVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    ReportSixVo vo = new ReportSixVo();
                    vo.setStorageDate(value.containsKey("入库日期") ? this.dealDateFormatOfNowOfStringJudge(value.get("入库日期").toString()) : null);
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setQty(new BigDecimal(value.containsKey("数量") ? value.get("数量").toString() : "0").setScale(2));
                    vo.setClient(value.containsKey("客户") ? value.get("客户").toString() : null);
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----6  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportSixVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfSeven(Triple<Boolean, Integer, JSONArray> remoteDataUnderway, String materialModel, String materialName) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportSevenVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    String specification = value.containsKey("规格型号") ? value.get("规格型号").toString() : "";
                    String materialNameTemp = value.containsKey("物料名称") ? value.get("物料名称").toString() : "";
                    if(!(specification.toLowerCase()).contains(ObjectUtil.isEmpty(materialModel)? "":materialModel.toLowerCase())){
                        continue;
                    }
                    if(!(materialNameTemp.toLowerCase()).contains(ObjectUtil.isEmpty(materialName)? "":materialName.toLowerCase())){
                        continue;
                    }
                    ReportSevenVo vo = new ReportSevenVo();
                    vo.setMaterialNumber(value.containsKey("物料代码") ? value.get("物料代码").toString() : null);
                    vo.setMaterialName(value.containsKey("物料名称") ? value.get("物料名称").toString() : null);
                    vo.setMaterialModel(value.containsKey("规格型号") ? value.get("规格型号").toString() : null);
                    vo.setBatchNo(value.containsKey("批次号") ? value.get("批次号").toString() : "");
                    vo.setUnitName(value.containsKey("单位") ? value.get("单位").toString() : null);
                    vo.setQty(new BigDecimal(value.containsKey("数量") ? value.get("数量").toString() : "0").setScale(2));
                    vo.setStock(value.containsKey("仓库") ? value.get("仓库").toString() : null);
                    vo.setFreightSpace(value.containsKey("仓位") ? value.get("仓位").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----7  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportSevenVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfEight(Triple<Boolean, Integer, JSONArray> remoteDataUnderway) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportEightVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    ReportEightVo vo = new ReportEightVo();
                    vo.setClientName(value.containsKey("客户") ? value.get("客户").toString() : null);
                    vo.setOneAmount(new BigDecimal(value.containsKey("一季度") ? value.get("一季度").toString() : "0"));
                    vo.setTwoAmount(new BigDecimal(value.containsKey("二季度") ? value.get("二季度").toString() : "0"));
                    vo.setThreeAmount(new BigDecimal(value.containsKey("三季度") ? value.get("三季度").toString() : "0"));
                    vo.setFourAmount(new BigDecimal(value.containsKey("四季度") ? value.get("四季度").toString() : "0"));
                    vo.createAmountList(new BigDecimal(value.containsKey("一季度") ? value.get("一季度").toString() : "0"),
                            new BigDecimal(value.containsKey("二季度") ? value.get("二季度").toString() : "0"),
                            new BigDecimal(value.containsKey("三季度") ? value.get("三季度").toString() : "0"),
                            new BigDecimal(value.containsKey("四季度") ? value.get("四季度").toString() : "0") );

                    vo.setAmount(new BigDecimal(value.containsKey("年度总计") ? value.get("年度总计").toString() : "0"));
                    vo.setProportion(value.containsKey("占比%") ? value.get("占比%").toString() : null);
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----8  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportEightVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }

    @Override
    public Triple<Boolean, Integer, JSONArray> processingDataOfNine(Triple<Boolean, Integer, JSONArray> remoteDataUnderway) {
        JSONArray right = remoteDataUnderway.getRight();
        List<ReportNineVo> result = new ArrayList<>();
        if (right.size() > 0) {
            for (int i = 0; i < right.size(); i++) {
                try {
                    JSONObject value = JSONObject.parseObject(new String(right.get(i).toString()));
                    ReportNineVo vo = new ReportNineVo();
                    vo.setIndex(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : null));
                    vo.setProductName(value.containsKey("产品名称") ? value.get("产品名称").toString() : null);
                    vo.setOneQty(new BigDecimal(value.containsKey("一季度") ? value.get("一季度").toString() : "0"));
                    vo.setTwoQty(new BigDecimal(value.containsKey("二季度") ? value.get("二季度").toString() : "0"));
                    vo.setThreeQty(new BigDecimal(value.containsKey("三季度") ? value.get("三季度").toString() : "0"));
                    vo.setFourQty(new BigDecimal(value.containsKey("四季度") ? value.get("四季度").toString() : "0"));
                    vo.createQtyList(new BigDecimal(value.containsKey("一季度") ? value.get("一季度").toString() : "0"),
                            new BigDecimal(value.containsKey("二季度") ? value.get("二季度").toString() : "0"),
                            new BigDecimal(value.containsKey("三季度") ? value.get("三季度").toString() : "0"),
                            new BigDecimal(value.containsKey("四季度") ? value.get("四季度").toString() : "0") );
                    vo.setQty(new BigDecimal(value.containsKey("年度总计") ? value.get("年度总计").toString() : "0"));
                    vo.setFrecords(Integer.parseInt(value.containsKey("FRECORDS") ? value.get("FRECORDS").toString() : "0"));
                    result.add(vo);
                } catch (Exception e) {
                    log.info("----9  数据解析出现异常：");
                }
            }
            if (!CollectionUtils.isEmpty(result)) {
                List<ReportNineVo> collect = result.stream().sorted(Comparator.comparing(e -> e.getFrecords())).collect(Collectors.toList());
                return Triple.of(true, remoteDataUnderway.getMiddle(), JSONArray.parseArray(JSON.toJSONString(collect)));
            }
        }
        return Triple.of(false, remoteDataUnderway.getMiddle(), null);
    }


}
